#!/usr/bin/perl
# 
# Exercise 9.2
# 
# Modify Exercise 9.1 to read in, and make a hash of, the entire REBASE restriction 
# site data from the bionet file.
#
# Answer to Exercise 9.2

# This is a bit of a trick question.  As you see, I've simply changed the file
# "bionet' (our short version of the data) to the file 'bionet.110' (the current
# version of the bionet data).  Our subroutine parseREBASE works just fine on
# it.  However, you may have interpreted the question differently. For instance,
# a good extension to our previous code would be to handle the alternate names
# for the enzymes, that appear in parentheses in the bionet file.  That would
# require a fairly simple modification of the parseREBASE subroutine - try it!

use strict;
use warnings;
use BeginPerlBioinfo;     # see Chapter 6 about this module

# Declare and initialize variables
my %rebase_hash = (  );
my @file_data = (  );
my $query = '';
my $dna = '';
my $recognition_site = '';
my $regexp = '';
my @locations = (  );

##################################################
#
# Here is the modification to Example 9-3
#
##################################################

# If there is a command-line argument, assume it's DNA
if(@ARGV) {
    $dna = $ARGV[0];

# Otherwise, prompt for a FASTA file to open
}else{

    print "Input a FASTA filename: ";
    my $filename = <STDIN>;
    chomp $filename;

    unless(-e $filename) {
    	print "$filename does not exist!\n";
	exit;
    }
    # Read in the file 
    @file_data = get_file_data($filename);

    # Extract the DNA sequence data from the contents of the file "sample.dna"
    $dna = extract_sequence_from_fasta_data(@file_data);
}

##################################################
#
# Here ends the modification
#
##################################################

##################################################
#
# Here is the modification to Exercise 9-1
#
##################################################

# Get the REBASE data into a hash, from file "bionet"
%rebase_hash = parseREBASE('bionet.110');

##################################################
#
# Here ends the modification
#
##################################################

# Prompt user for restriction enzyme names, create restriction map
do {
    print "Search for what restriction site (or quit)?: ";
    
    $query = <STDIN>;

    chomp $query;

    # Exit if empty query
    if ($query =~ /^\s*$/ ) {

        exit;
    }

    # Perform the search in the DNA sequence
    if ( exists $rebase_hash{$query} ) {

        ($recognition_site, $regexp) = split ( " ", $rebase_hash{$query});

        # Create the restriction map
        @locations = match_positions($regexp, $dna);

        # Report the restriction map to the user
        if (@locations) {
            print "Searching for $query $recognition_site $regexp\n";
            print "A restriction site for $query at locations:\n";
            print join(" ", @locations), "\n";
        } else {
            print "A restriction enzyme $query is not in the DNA:\n";
        }
    }
    print "\n";
} until ( $query =~ /quit/ );

exit;

################################################################################
#
# Subroutine
#
# Find locations of a match of a regular expression in a string
#
# 
# return an array of positions where the regular expression
#  appears in the string
#

sub match_positions {

    my($regexp, $sequence) = @_;

    use strict;

    use BeginPerlBioinfo;     # see Chapter 6 about this module

    #
    # Declare variables
    #

    my @positions = (  );

    #
    # Determine positions of regular expression matches
    #
    
    while ( $sequence =~ /$regexp/ig ) {

        push ( @positions, pos($sequence) - length($&) + 1);
    }

    return @positions;
}
